/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC 1T Series MCU Demo Programme -------------------------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966 ------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.STCMCUDATA.com  ---------------------------------------*/
/* --- QQ:  800003751 -------------------------------------------------*/
/* ���Ҫ�ڳ�����ʹ�ô˴���,���ڳ�����ע��ʹ����STC�����ϼ�����            */
/*---------------------------------------------------------------------*/

/***************	����˵��	****************

���ļ�ΪSTC8ϵ�е���ʱ����,�û��������Բ��޸��������.

******************************************/

#include "rs485.h"
#include "delay.h"
#include "UART.h"
#include "MainTask.h"

unsigned char Q_RS485_Receive_read = 0;

unsigned char xdata RS485RxBuffer[32];
unsigned char xdata RS485TxBuffer[32];
unsigned char receive_485_counter = 0;

#define RS485_SendBytes RS485_put_buf

#define RS485 1

void WaitRS485()
{
#if 1
	delay_ms(2);
#else
	int i = 39;
	while (i--)
	{
		Delay(500);
	}
#endif
}

/*======================
  1������һ������
========================*/
void RS485_put_buf(unsigned char *str, unsigned char lenght)
{
	u8 i;
	for (i = 0; i < lenght; i++)
#ifdef RS485
		TX2_write2buff(str[i]); //д�뷢�ͻ��壬ָ��+1
#else
		TX1_write2buff(str[i]); //д�뷢�ͻ��壬ָ��+1
#endif
}

unsigned char Chack_Xor(unsigned char *Buffer, unsigned char len)
{
	unsigned char Data = 0;
	while (len-- > 0)
		Data ^= *Buffer++;
	return Data;
}

u8 SendFlag = 0;
/*======================
1�������յ�������
========================*/
void rs485_data_receive(void)
{
	unsigned char tmp;

	// TX2_write2buff('A'); //д�뷢�ͻ��壬ָ��+1

	if (!SendFlag)
	{
		if (P36 == 1)
		{
#ifdef RS485
			while (COM2.RX_Cnt != Q_RS485_Receive_read)
			{
				tmp = RX2_Buffer[Q_RS485_Receive_read];
				Q_RS485_Receive_read = (Q_RS485_Receive_read + 1) % COM_RX2_Lenth;
#else
			while (COM1.RX_Cnt != Q_RS485_Receive_read)
			{
				tmp = RX1_Buffer[Q_RS485_Receive_read];
				Q_RS485_Receive_read = (Q_RS485_Receive_read + 1) % COM_RX1_Lenth;
#endif
				switch (receive_485_counter)
				{
				case 0:
					if (tmp == 0xaa)
					{
						RS485RxBuffer[receive_485_counter] = tmp;
						receive_485_counter++;
					}
					else
					{
						receive_485_counter = 0;
					}
					break;
				case 1:
					if (tmp == 0xaa)
					{
						RS485RxBuffer[receive_485_counter] = tmp;
						receive_485_counter++;
					}
					else
					{
						receive_485_counter = 0;
					}
					break;

				case 8:
					RS485RxBuffer[receive_485_counter] = tmp;
					//if(tmp==0x55)
					if ((tmp == 0x55) || (Chack_Xor(RS485RxBuffer, 9) == 0))
					{
						RS485TxBuffer[0] = 0xaa;
						RS485TxBuffer[1] = 0xaa;
						if (RS485RxBuffer[7] == 0x32)
						{
							//��Դ�壬0x30 �Ž���,0x31 �¿ذ壬0x32 ��Դ��

							//�� aa aa 00 31 01 00 00 32 55
							//�� aa aa 00 31 00 00 00 32 55
							switch (RS485RxBuffer[3])
							{
							case 0x00:
								// RS485TxBuffer[0] = 0xaa;
								// RS485TxBuffer[1] = 0xaa;

								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = Power_In;
								RS485TxBuffer[4] = AdapterSwitchFlag; //

								RS485TxBuffer[5] = (Voltage / 10) >> 0;
								RS485TxBuffer[6] = (Voltage / 10) >> 8;

								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 0x31: //�̵���//ԭ������

								Fun(RS485RxBuffer[4]);

								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = Power_In;
								RS485TxBuffer[4] = AdapterSwitchFlag; //

								RS485TxBuffer[5] = (Voltage / 10) >> 0;
								RS485TxBuffer[6] = (Voltage / 10) >> 8;

								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 0x30:// �ɿص�Դ
							case 0x32:
								Power_Out = RS485RxBuffer[3];

								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = Power_In;
								RS485TxBuffer[4] = AdapterSwitchFlag; //

								RS485TxBuffer[5] = (Voltage / 10) >> 0;
								RS485TxBuffer[6] = (Voltage / 10) >> 8;

								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 0x33:
								Fun(RS485RxBuffer[4]);

								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = Power_In;
								RS485TxBuffer[4] = AdapterSwitchFlag; //

								RS485TxBuffer[5] = (Voltage / 10) >> 0;
								RS485TxBuffer[6] = (Voltage / 10) >> 8;

								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 0x35:											//0x00: ��ѯ����״̬ 0x01����(����ָ��) 0x02���ر�(����ָ��)
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = RS485RxBuffer[3];
								if (RS485RxBuffer[4] == 0)
								{
									// ��ѯ����״̬ 0x00:ͨ��״̬ 0x01:�ϵ�״̬
									if (getControllableStatus())
									{
										RS485TxBuffer[4] = 0;
									}
									else
									{
										RS485TxBuffer[4] = 1;
									}
								}
								else 
								{	
									//��/�رտ���
									setControllablePower(RS485RxBuffer[4]);
									// ��ѯ����״̬ 0x00:ͨ��״̬ 0x01:�ϵ�״̬
									if (getControllableStatus())
									{
										RS485TxBuffer[4] = 0;
									}
									else
									{
										RS485TxBuffer[4] = 1;
									}
								}
								RS485TxBuffer[5] = (Voltage / 10) >> 0;
								RS485TxBuffer[6] = (Voltage / 10) >> 8;
								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							default:
								;
							}
							RS485TxBuffer[8] = Chack_Xor(RS485TxBuffer, 8);
							SendFlag = 1;
						}
						else if (RS485RxBuffer[7] == 0x36) //��ʪ�ȶ�ȡ
						{

							switch (RS485RxBuffer[6])
							{
							case 1: //��ѯʪ��
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = (AM2321_humi >> 8) & 0xff; //ʪ�ȸ��ֽ�
								RS485TxBuffer[4] = AM2321_humi & 0xff;		  //ʪ�ȵ��ֽ�
								RS485TxBuffer[5] = chushiqi_real_state;
								RS485TxBuffer[6] = RS485RxBuffer[6];
								RS485TxBuffer[7] = RS485RxBuffer[7];

								break;

							case 2: //��ѯ�¶�
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = (AM2321_temp >> 8) & 0xff; //ʪ�ȸ��ֽ�
								RS485TxBuffer[4] = AM2321_temp & 0xff;		  //ʪ�ȵ��ֽ�
								RS485TxBuffer[5] = chushiqi_real_state;
								RS485TxBuffer[6] = RS485RxBuffer[6];
								RS485TxBuffer[7] = RS485RxBuffer[7];

								break;

							case 3: //���Ƴ�ʪ��
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = 0;
								RS485TxBuffer[4] = 0;
								//chushiqi_want_state = RS485RxBuffer[5];
								RS485TxBuffer[5] = chushiqi_real_state;
								RS485TxBuffer[6] = RS485RxBuffer[6];
								RS485TxBuffer[7] = RS485RxBuffer[7];
								ChuShi(RS485RxBuffer[5]);
								break;

							case 4: //����
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = (ChargingFlag << 1) + AdapterSwitchFlag;
								RS485TxBuffer[4] = Voltage / 100;
								RS485TxBuffer[5] = Current / 10;
								RS485TxBuffer[6] = 4;
								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 5: //����

								Alarm(RS485RxBuffer[5]);
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = RS485RxBuffer[3];
								RS485TxBuffer[4] = RS485RxBuffer[4];
								RS485TxBuffer[5] = RS485RxBuffer[5];
								RS485TxBuffer[6] = RS485RxBuffer[6];
								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 6: //����

								Fun(RS485RxBuffer[5]);
								RS485TxBuffer[2] = RS485RxBuffer[2];
								RS485TxBuffer[3] = RS485RxBuffer[3];
								RS485TxBuffer[4] = RS485RxBuffer[4];
								RS485TxBuffer[5] = RS485RxBuffer[5];
								RS485TxBuffer[6] = RS485RxBuffer[6];
								RS485TxBuffer[7] = RS485RxBuffer[7];
								break;

							case 0x38:
								//aa aa xx 38 00 00 80 30 55//��ȡ�汾

								if (RS485RxBuffer[2] == Address)
								{
									RS485TxBuffer[0] = 0xaa;
									RS485TxBuffer[1] = 0xaa;
									RS485TxBuffer[2] = Address;
									RS485TxBuffer[3] = 0x38;
									RS485TxBuffer[4] = VERSION / 256;
									RS485TxBuffer[5] = VERSION;
									RS485TxBuffer[6] = 0x80;
									RS485TxBuffer[7] = 0X30;
									RS485TxBuffer[8] = Chack_Xor(RS485TxBuffer, 8);
								}

								break;

							default:
								;
							}

							RS485TxBuffer[8] = Chack_Xor(RS485TxBuffer, 8);
							SendFlag = 1;
						}
						// else if (RS485RxBuffer[7] == 0x30)  //
						// {
						// 	if (RS485RxBuffer[2] == 0 || RS485RxBuffer[2] == 0xff)
						// 	{
						// 		if (RS485RxBuffer[3] == 0x50) //
						// 		{
						// 			RS485TxBuffer[2] = RS485RxBuffer[2];
						// 			RS485TxBuffer[3] = RS485RxBuffer[3];
						// 			RS485TxBuffer[4] = (GPIO_ReadInputData(GPIOB) >> 5) & 0x1f;
						// 			RS485TxBuffer[5] = 0;
						// 			RS485TxBuffer[6] = 0;
						// 			RS485TxBuffer[7] =  RS485RxBuffer[7];
						// 			RS485TxBuffer[8] = Chack_Xor(RS485TxBuffer, 8);
						// 			RS485_put_buf((unsigned char *)&RS485TxBuffer, 9);
						// 			break;

						// 		}
						// 	}

						// }
					}

					receive_485_counter = 0;
					break;
				default:
					RS485RxBuffer[receive_485_counter] = tmp;
					receive_485_counter++;
				}
			}
		}
		else
		{

#ifdef RS485
			while (COM2.RX_Cnt != Q_RS485_Receive_read)
			{
				tmp = RX2_Buffer[Q_RS485_Receive_read];
				Q_RS485_Receive_read = (Q_RS485_Receive_read + 1) % COM_RX2_Lenth;
#else
			while (COM1.RX_Cnt != Q_RS485_Receive_read)
			{
				tmp = RX1_Buffer[Q_RS485_Receive_read];
				Q_RS485_Receive_read = (Q_RS485_Receive_read + 1) % COM_RX1_Lenth;
#endif
				switch (receive_485_counter)
				{
				case 0:
					if (tmp == 0xaa)
					{
						RS485RxBuffer[receive_485_counter] = tmp;
						receive_485_counter++;
					}
					else
					{
						receive_485_counter = 0;
					}
					break;
				case 1:
					if (tmp == 0xaa)
					{
						RS485RxBuffer[receive_485_counter] = tmp;
						receive_485_counter++;
					}
					else
					{
						receive_485_counter = 0;
					}
					break;

				case 6:
					if (RS485RxBuffer[5] == SHEBEI)
					{
						//��Դ�壬0x30 �Ž���,0x31 �¿ذ壬0x32 ��Դ��

						if ((tmp == 0x55) || (Chack_Xor(RS485RxBuffer, 9) == 0))
						{
							RS485TxBuffer[0] = 0xaa;
							RS485TxBuffer[1] = 0xaa;
							Power_Out = RS485RxBuffer[3];
							RS485TxBuffer[2] = RS485RxBuffer[2];
							RS485TxBuffer[3] = Power_In;
							RS485TxBuffer[4] = AdapterSwitchFlag;
							RS485TxBuffer[5] = SHEBEI;
							SendFlag = 1;
						}
					}
					receive_485_counter = 0;
					break;
				default:
					RS485RxBuffer[receive_485_counter] = tmp;
					receive_485_counter++;
					break;
				}
			}
		}
	}
}

void Rs485Send()
{
	if (SendFlag)
	{
		SendFlag = 0;
		if (P36 == 1)
		{
			RS485_put_buf((unsigned char *)&RS485TxBuffer, 9);
		}
		else
		{
			RS485_put_buf((unsigned char *)&RS485TxBuffer, 7);
		}
	}
}
